package com.dks.util;

import java.io.File;
import java.net.URLDecoder;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import org.slf4j.LoggerFactory;

/**
 * @Author: zn
 * @Description: 生成二维码
 * @Date: 15:39 2020/5/13
 * @Modificd By:
 * @Param:
 * @return:
 * @throw: 请描述异常信息
 */
public class CreateQRCodeUtil {

    private final org.slf4j.Logger logger = LoggerFactory.getLogger(getClass());

//    public String getbfQrcode(String bfcode) {
//
//        logger.info("接收到的二维码信息内容：" + bfcode);
//
//        if (bfcode.contains("%7D=")) {
//            bfcode = bfcode.substring(0, bfcode.length() - 1);
//            try {
//                bfcode = URLDecoder.decode(bfcode, "UTF-8");
//            } catch (Exception E) {
//                logger.info("二维码信息解码失败");
//            }
//        }
//        //判断url 携带 :// url 编码为 %3A%2F%2F
//        if (bfcode.contains("%3A%2F%2F")) {
//            bfcode = bfcode.substring(0, bfcode.length() - 1);
//            try {
//                bfcode = URLDecoder.decode(bfcode, "UTF-8");
//            } catch (Exception E) {
//                logger.info("二维码信息解码失败");
//            }
//        }
//
//        String s = null;
//        try {
//            s = CreateQRCode(bfcode);
//        } catch (Exception e) {
//            logger.info("getbf-qrcode 异常信息{}", e.getMessage(), e);
//        }
//
//        return ResultFactory.resultMsg(Commond.R_SUCCESS, s, "图片上传成功");
//
//    }

    public String CreateQRCode(Integer userId) throws Exception {
//        System.out.println("CreateQRCode:37");
        final int width = 300;
        final int height = 300;
        final String format = "png";
        final String content = String.valueOf(userId);

        //定义二维码的参数
        HashMap hints = new HashMap();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
        hints.put(EncodeHintType.MARGIN, 2);
        Date date = new Date();
        String basePath = "upload/";
        File baseFile = new File(basePath);
        String imgName = basePath + new SimpleDateFormat("yyyyMMddhhmmss").format(date) + String.valueOf((int) (Math.random() * (9999 - 1000 + 1)) + 1000) + ".png";
        String img = "";

        //如果文件夹不存在则创建
        if (!baseFile.exists() && !baseFile.isDirectory()) {
            baseFile.mkdir();
        }

//            System.out.println("CreateQRCode:57");
        //OutputStream stre am = new OutputStreamWriter();
        BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
        File f = new File(imgName);
        Path file = f.toPath();
        MatrixToImageWriter.writeToPath(bitMatrix, format, file);
        QiniuCloudUtil qiniuCloudUtil = new QiniuCloudUtil();
        String url = QiniuCloudUtil.upload(f, f.getName());

        f.delete();
        //MatrixToImageWriter.writeToStream(bitMatrix, format, stream);

//        System.out.println("CreateQRCode:69---"+img);
        return url;
    }


}